package jp.tuyano.sample.client.reference;

import java.util.List;

import jp.tuyano.sample.client.form.ReferenceForm;
import jp.tuyano.sample.shared.DataConvert;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Reference implements EntryPoint {

    private final ReferenceServiceAsync referenceService =
        GWT.create(ReferenceService.class);

    
    
    public void onModuleLoad() {

        referenceService.getAllData(new AsyncCallback<List<ReferenceForm>>() {

           public void onSuccess(List<ReferenceForm> result) {
                if (!result.isEmpty()) {
                    VerticalPanel panel = new VerticalPanel();
                    panel.setStyleName("reference_table");
                    Label label = new Label("全従業員のデータを表示します。");
                    panel.add(label);
                    FlexTable table = new FlexTable();
                    table.setBorderWidth(2);
                    table.setText(0, 0, "ID");
                    table.setText(0, 1, "氏");
                    table.setText(0, 2, "名");
                    table.setText(0, 3, "性別");
                    table.setText(0, 4, "入社日");
                    for (int i=0; i<result.size(); i++) {
                        ReferenceForm form = result.get(i);
                        table.setText(i+1, 0, form.getId());
                        table.setText(i+1, 1, form.getLastName());
                        table.setText(i+1, 2, form.getFirstName());
                        table.setText(i+1, 3, DataConvert.convertDisplaySex(
                                form.getSex()));
                        table.setText(i+1, 4, form.getEnterDate());
                    }
                    panel.add(table);
                    RootPanel.get("reference").add(panel);
                } else {
                    Label emptylabel = new Label("Empty.");
                    RootPanel.get("reference").add(emptylabel);
                }
            }
            
            public void onFailure(Throwable caught) {
                
                Label errorlabel = new Label("SEVER ERROR!!");
                RootPanel.get("reference").add(errorlabel);
            }
        });
        
    }
}
